在我們前篇文章中簡單的介紹了 emptyDir 並提到他與 Pod 的生命週期共生共滅,所以他通常被用於數據緩存或者臨時存儲的場景,接下來就來實際操作練習一下吧。
emptyDir 可以簡單理解為在 Pod 運作時的一個臨時目錄,就像我們在 Docker 上使用的 Volume ,而在 Pod 被移除時會被一併刪除,除了一些特殊場景通常我們都會將他用於在一個 Pod 內的多個容器間的文件的共享,或做為容器數據的臨時緩存存儲目錄等等。
emptyDir存儲卷則定義於. spec.volumes.emptyDir 嵌套字段中,可用字段主要包含兩個,具體如下:
apiVersion: v1
kind: Pod
metadata:
  name: emptydir-pod
spec:
  volumes:
    - name: html
      emptyDir: {}
  containers:
    - name: nginx
      image: nginx:latest
      volumeMounts:
        - name: html
          mountPath: /usr/share/nginx/html
    - name: alpine
      image: alpine
      volumeMounts:
        - name: html
          mountPath: /html
      command: [ "/bin/sh", "-c" ]
      args: # 每十秒定時向 /html/index.html 寫入資料
        - while true; do
          echo $(hostname) $(date) >> /html/index.html;
          sleep 10;
          done
我們可以很簡單的看到再 Pod 的設定檔中,我們啟用了 nginx 以及 alpine 並且掛載同一個的 emptyDir 的共享目錄
kubectl apply -f emptydir.yaml
--------
pod/emptydir-pod created
kubectl describe pod/emptydir-pod
--------
Containers:
  nginx:
    Container ID:   docker://64883c01c4e987beaa4cfbda1bba5cbe571b934dcc47b978e4adca4569a21170
    Image:          nginx:latest
    Image ID:       docker-pullable://nginx@sha256:1761fb5661e4d77e107427d8012ad3a5955007d997e0f4a3d41acc9ff20467c7
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Tue, 26 Jul 2022 17:05:41 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /usr/share/nginx/html from html (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-bmdwh (ro)
  alpine:
    Container ID:  docker://7881eb57f048f56e9d9ed4eedab818aaf876138cc488bef79746008c2a1047e9
    Image:         alpine
    Image ID:      docker-pullable://alpine@sha256:7580ece7963bfa863801466c0a488f11c86f85d9988051a9f9c68cb27f6b7872
    Port:          <none>
    Host Port:     <none>
    Command:
      /bin/sh
      -c
    Args:
      while true; do echo $(hostname) $(date) >> /html/index.html; sleep 10; done
    State:          Running
      Started:      Tue, 26 Jul 2022 17:05:47 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /html from html (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-bmdwh (ro)
	Conditions:
	  Type              Status
	  Initialized       True 
	  Ready             True 
	  ContainersReady   True 
	  PodScheduled      True 
	Volumes:
	  html:
	    Type:       EmptyDir (a temporary directory that shares a pod's lifetime)
	    Medium:     
	    SizeLimit:  <unset>
可以從返回結果中看到各個容器與 Volume 的掛載狀態。
來確認一下 alpine 容器每隔 10 秒像 html/index.html 寫入訊息,而 Nginx 容器掛載的 emptyDir 是否同時也可以取得更新。
將 port 導出到本地的 localhost :
kubectl port-forward pod/emptydir-pod 8080:80
-------
Forwarding from 127.0.0.1:8080 -> 80pod 8080:80
Forwarding from [::1]:8080 -> 80
使用 curl 查看返回值:
curl http://localhost:8080
--------
emptydir-pod Tue Jul 26 09:05:47 UTC 2022
emptydir-pod Tue Jul 26 09:05:57 UTC 2022
emptydir-pod Tue Jul 26 09:06:07 UTC 2022
emptydir-pod Tue Jul 26 09:06:17 UTC 2022
emptydir-pod Tue Jul 26 09:06:27 UTC 2022
順利取得由 alpine 容器產生的內容~
通過 -c 可以指定容器名稱進入指定容器
kubectl exec -it pods/emptydir-pod -c nginx -- sh
head -3 /usr/share/nginx/html/index.html
--------
emptydir-pod Tue Jul 26 09:05:47 UTC 2022
emptydir-pod Tue Jul 26 09:05:57 UTC 2022
emptydir-pod Tue Jul 26 09:06:07 UTC 2022
kubectl exec -it pods/emptydir-pod -c alpine -- sh
head -3 /html/index.html
--------
emptydir-pod Tue Jul 26 09:05:47 UTC 2022
emptydir-pod Tue Jul 26 09:05:57 UTC 2022
emptydir-pod Tue Jul 26 09:06:07 UTC 2022
ps aux
--------
PID   USER     TIME  COMMAND
    1 root      0:00 /bin/sh -c while true; do echo $(hostname) $(date) >> /html/index.html; sleep 10; done
  371 root      0:00 sh
  395 root      0:00 sleep 10
  396 root      0:00 ps aux
apiVersion: v1
kind: Pod
metadata:
  name: emptydir-memory-pod
spec:
  volumes:
    - name: html
      emptyDir:
        medium: Memory                #指定使用記憶體儲存
        sizeLimit: 256Mi              #限制內存大小
  containers:
    - name: nginx
      image: nginx:latest
      volumeMounts:
        - name: html
          mountPath: /usr/share/nginx/html
    - name: alpine
      image: alpine
      volumeMounts:
        - name: html
          mountPath: /html
      command: [ "/bin/sh", "-c" ]
      args:
        - while true; do
          echo $(hostname) $(date) >> /html/index.html;
          sleep 10;
          done
上面簡單的例子充分的展現出 emptyDir 特有的定位以及簡單易懂的用法,但這些完全只是 Kubernetes Volumes 中多種類的其中之一而已,隨後還會有幾篇文章來介紹其他常用的 Volume 。
千呼萬喚始出來!鐵人賽系列「從異世界歸來發現只剩自己不會 Kubernetes」同名改編作品出版了!
感謝所有交流指教的各路英雄,也感謝願意點閱文章的各位,如果能幫助到任何人都將會是我的榮幸。
本書內容改編自第 14 屆 iThome 鐵人賽 DevOps 組的優選系列文章《從異世界歸來發現只剩自己不會 Kubernetes》。此書是一本綜合性的指南,針對想要探索認識 Kubernetes 的技術人員而生。無論是初涉此領域的新手,還是已有深厚經驗的資深工程師,本書都能提供你所需的知識和技能。
「這本書不僅提供了豐富的範例程式碼和操作指南,讓身為工程師的我們能實際操作來加深認知;更重要的是,它教會我如何從後端工程師的角度去思考和應用 Kubernetes。從容器的生命週期、資源管理到部署管理,每一章都與我們的日常開發工作息息相關。」
──── 雷N │ 後端工程師 / iThome 鐵人賽戰友
天瓏連結: 從異世界歸來發現只剩自己不會 Kubernetes:初心者進入雲端世界的實戰攻略!
相關文章:
相關程式碼同時收錄在:
https://github.com/MikeHsu0618/2022-ithelp/tree/master/Day17
Reference
請問1. 創建一個有多個容器的 Pod 那裏,是nginx 和 apline 共用kubernetes 的暫存空間?
請問3. 查看 Pod 狀態 的Mounts: /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-bmdwh 是kubernetes 的暫存空間位置?
請問3. 查看 Pod 狀態 Mounts: /usr/share/nginx/html from html (rw) 最後面的 from html 是containers.volumeMounts.name: html 名字?
Default service account 綁定在每個 container 中。